package hanjia;

import suanfa.ListNode;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/2/4-下午5:03
 * Since  :   1.0
 * Version:   1.0
 */
public class LeetCode83 {
    // 112344
    //result 1234
    public ListNode deleteDuplicates(ListNode head) {
        //节点数小于等于2
        if (head==null||head.next==null)
        {
            return head;
        }

        ListNode p1=head;
        ListNode p2=head.next;
        while (p2!=null){

            if (p1.val==p2.val){
                p1.next=p2.next;
            }else {
                p1=p1.next;

            }
            p2=p2.next;

        }
        return head;




    }

    public ListNode deleteDuplicates2(ListNode head){
        if (head==null||head.next==null){
            return  head;
        }

          if (head.val==head.next.val){
              return  deleteDuplicates2(head.next);
          }else {
              head.next=deleteDuplicates2(head.next);
              return head;
          }


    }

//112344
//23 去掉重复节点
    public ListNode deleteDuplicates3(ListNode head) {
        if (head==null||head.next==null){
            return  head;
        }
        if (head.val==head.next.val){
            while (head.val==head.next.val&&head.next!=null){
                head=head.next;
            }
            return deleteDuplicates3(head.next);
        }else {
            head.next=deleteDuplicates3(head.next);
            return  head;


        }


    }


    public ListNode deleteDuplicates4(ListNode head){
        if (head==null||head.next==null){
            return  head;
        }

        ListNode listNode = new ListNode();
        listNode.next=head;
        ListNode p1=listNode;
        ListNode p2=head;
        ListNode p3=head.next;
        while (p3!=null){

            if (p3.val==p2.val){
                while (p3!=null&&p3.val==p2.val){

                    p3=p3.next;
                }
                p1.next=p3;

                p2=p3;


            }else {
                p1=p2;
                p2=p2.next;

            }

            p3=p3.next;

        }
        return listNode.next;




    }
}
